package cn.ustb.serviceImpl;

import cn.ustb.dao.StudentGradeDao;
import cn.ustb.entity.StudentGrade;
import cn.ustb.entity.User;
import cn.ustb.service.StudentGradeService;
import cn.ustb.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author yuyan
 */
public class StudentGradeServiceImpl implements StudentGradeService {

    @Override
    public List<StudentGrade> getAllcourseGradeList(User user) {
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getInstance();
            StudentGradeDao studentGradeDao=sqlSession.getMapper(StudentGradeDao.class);
            return studentGradeDao.getAllCourseGrade(Long.parseLong(user.getUsername()));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
        return null;
    }

    @Override
    public List<StudentGrade> getAllWithYearTerm(Long studentId, String yearTerm) {
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getInstance();
            StudentGradeDao studentGradeDao=sqlSession.getMapper(StudentGradeDao.class);
            return studentGradeDao.getAllWithYearTerm(studentId, yearTerm);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
        return null;
    }

    @Override
    public List<StudentGrade> getAll() {
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getInstance();
            StudentGradeDao studentGradeDao=sqlSession.getMapper(StudentGradeDao.class);
            return studentGradeDao.getAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
        return null;
    }

    @Override
    public void insert(StudentGrade studentGrade) {
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getInstance();
            StudentGradeDao studentGradeDao=sqlSession.getMapper(StudentGradeDao.class);
            studentGradeDao.insert(studentGrade);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
    }

    @Override
    public StudentGrade getStudentGrade(StudentGrade studentGrade) {
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getInstance();
            StudentGradeDao studentGradeDao=sqlSession.getMapper(StudentGradeDao.class);
            return studentGradeDao.getStudentGrade(studentGrade);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
        return null;
    }

    @Override
    public void update(StudentGrade studentGrade) {
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getInstance();
            StudentGradeDao studentGradeDao=sqlSession.getMapper(StudentGradeDao.class);
            studentGradeDao.update(studentGrade);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
    }

    @Override
    public List<StudentGrade> getBiXiucourseGradeList(User user) {
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getInstance();
            StudentGradeDao studentGradeDao=sqlSession.getMapper(StudentGradeDao.class);
            return studentGradeDao.getBiXiuCourseGrade(Long.parseLong(user.getUsername()));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
        return null;
    }

    @Override
    public List<StudentGrade> getXuanXiucourseGradeList(User user) {
        SqlSession sqlSession=null;
        try{
            sqlSession= MyBatisUtil.getInstance();
            StudentGradeDao studentGradeDao=sqlSession.getMapper(StudentGradeDao.class);
            return studentGradeDao.getXuanXiuCourseGrade(Long.parseLong(user.getUsername()));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
        return null;
    }
}
